//
//  XKRegisterView.m
//  xk100
//
//  Created by xiaokang on 2017/6/23.
//  Copyright © 2017年 xiaokang. All rights reserved.
//

#import "XKRegisterView.h"

@interface XKRegisterView () <UITextFieldDelegate>

@property (nonatomic, strong) UIScrollView *backgroundScrollView;

@property (nonatomic, strong) UIView *backgroundView_nickname;
@property (nonatomic, strong) UIView *backgroundView_phone;
@property (nonatomic, strong) UIView *backgroundView_vcode;
@property (nonatomic, strong) UIView *backgroundView_password;
@property (nonatomic, strong) UIView *backgroundView_passwordConfirm;
@property (nonatomic, strong) UIView *backgroundView_inviter;

@property (nonatomic, strong) UILabel *titleLabel_nickname;
@property (nonatomic, strong) UILabel *titleLabel_phone;
@property (nonatomic, strong) UILabel *titleLabel_vcode;
@property (nonatomic, strong) UILabel *titleLabel_password;
@property (nonatomic, strong) UILabel *titleLabel_passwordConfirm;
@property (nonatomic, strong) UILabel *titleLabel_inviter;

@property (nonatomic, strong) UITextField *textField_nickname;
@property (nonatomic, strong) UITextField *textField_phone;
@property (nonatomic, strong) UITextField *textField_vcode;
@property (nonatomic, strong) UITextField *textField_password;
@property (nonatomic, strong) UITextField *textField_passwordConfirm;
@property (nonatomic, strong) UITextField *textField_inviter;

@property (nonatomic, strong) UIButton *registerButton;
@end
@implementation XKRegisterView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubviews];
    }
    return self;
}

#pragma mark -
- (void)initSubviews {
    
    _backgroundScrollView = [[UIScrollView alloc] init];
    _backgroundScrollView.showsVerticalScrollIndicator = NO;
    _backgroundScrollView.showsHorizontalScrollIndicator = NO;
    _backgroundScrollView.contentSize = CGSizeMake(Main_Width, Main_Height-64);
    
    
    _backgroundView_nickname = [UIView new];
    _backgroundView_nickname.backgroundColor = WhiteColor;
    
    _backgroundView_phone = [UIView new];
    _backgroundView_phone.backgroundColor = WhiteColor;
    
    _backgroundView_vcode = [UIView new];
    _backgroundView_vcode.backgroundColor = WhiteColor;
    
    _backgroundView_password = [UIView new];
    _backgroundView_password.backgroundColor = WhiteColor;
    
    _backgroundView_passwordConfirm = [UIView new];
    _backgroundView_passwordConfirm.backgroundColor = WhiteColor;
    
    _backgroundView_inviter = [UIView new];
    _backgroundView_inviter.backgroundColor = WhiteColor;
    
    
    [self addSubview:_backgroundScrollView];
    [self.backgroundScrollView addSubview:_backgroundView_nickname];
    [self.backgroundScrollView addSubview:_backgroundView_phone];
    [self.backgroundScrollView addSubview:_backgroundView_vcode];
    [self.backgroundScrollView addSubview:_backgroundView_password];
    [self.backgroundScrollView addSubview:_backgroundView_passwordConfirm];
    [self.backgroundScrollView addSubview:_backgroundView_inviter];
    
    [_backgroundScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(self);
    }];
    
    
    [_backgroundView_nickname mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.mas_equalTo(self.backgroundScrollView);
        make.height.mas_equalTo(50*Scale_W);
        make.top.mas_equalTo(self.backgroundScrollView).offset(20*Scale_W);
    }];
    
    [_backgroundView_phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(_backgroundView_nickname);
        make.top.mas_equalTo(_backgroundView_nickname.mas_bottom).offset(1);
    }];
    
    [_backgroundView_vcode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(_backgroundView_phone);
        make.top.mas_equalTo(_backgroundView_phone.mas_bottom).offset(1);
    }];
    
    [_backgroundView_password mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(_backgroundView_phone);
        make.top.mas_equalTo(_backgroundView_vcode.mas_bottom).offset(1);
    }];
    
    [_backgroundView_passwordConfirm mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(_backgroundView_phone);
        make.top.mas_equalTo(_backgroundView_password.mas_bottom).offset(1);
    }];
    
    [_backgroundView_inviter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(_backgroundView_phone);
        make.top.mas_equalTo(_backgroundView_passwordConfirm.mas_bottom).offset(1);
    }];
    
    _titleLabel_nickname = [UILabel getLabWithText:@"用  户  名:" textFont:Font_15 textColor:[UIColor darkGrayColor]];
    _titleLabel_phone = [UILabel getLabWithText:@"手  机  号:" textFont:Font_15 textColor:[UIColor darkGrayColor]];
    _titleLabel_vcode = [UILabel getLabWithText:@"验  证  码:" textFont:Font_15 textColor:[UIColor darkGrayColor]];
    _titleLabel_password = [UILabel getLabWithText:@"登录密码:" textFont:Font_15 textColor:[UIColor darkGrayColor]];
    _titleLabel_passwordConfirm = [UILabel getLabWithText:@"确认密码:" textFont:Font_15 textColor:[UIColor darkGrayColor]];
    _titleLabel_inviter = [UILabel getLabWithText:@"邀  请  人:" textFont:Font_15 textColor:[UIColor darkGrayColor]];
    
    [_backgroundView_nickname addSubview:_titleLabel_nickname];
    [_backgroundView_phone addSubview:_titleLabel_phone];
    [_backgroundView_vcode addSubview:_titleLabel_vcode];
    [_backgroundView_password addSubview:_titleLabel_password];
    [_backgroundView_passwordConfirm addSubview:_titleLabel_passwordConfirm];
    [_backgroundView_inviter addSubview:_titleLabel_inviter];
    
    [_titleLabel_nickname mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backgroundView_nickname).offset(30*Scale_W);
        make.top.bottom.mas_equalTo(_backgroundView_nickname);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [_titleLabel_phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backgroundView_phone).offset(30*Scale_W);
        make.top.bottom.mas_equalTo(_backgroundView_phone);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [_titleLabel_vcode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backgroundView_vcode).offset(30*Scale_W);
        make.top.bottom.mas_equalTo(_backgroundView_vcode);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [_titleLabel_password mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backgroundView_password).offset(30*Scale_W);
        make.top.bottom.mas_equalTo(_backgroundView_password);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [_titleLabel_passwordConfirm mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backgroundView_passwordConfirm).offset(30*Scale_W);
        make.top.bottom.mas_equalTo(_backgroundView_passwordConfirm);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [_titleLabel_inviter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backgroundView_inviter).offset(30*Scale_W);
        make.top.bottom.mas_equalTo(_backgroundView_inviter);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    
    _textField_nickname = [UITextField new];
    _textField_nickname.delegate = self;
    _textField_nickname.font = Font(16);
    _textField_nickname.borderStyle = UITextBorderStyleNone;
    _textField_nickname.placeholder = @"请输入用户名";
    [_textField_nickname addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    
    _textField_phone = [UITextField new];
    _textField_phone.delegate = self;
    _textField_phone.font = Font(16);
    _textField_phone.borderStyle = UITextBorderStyleNone;
    _textField_phone.keyboardType = UIKeyboardTypeNumberPad;
    _textField_phone.placeholder = @"请输入11位手机号";
    [_textField_phone addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    
    _textField_vcode = [UITextField new];
    _textField_vcode.delegate = self;
    _textField_vcode.font = Font(16);
    _textField_vcode.borderStyle = UITextBorderStyleNone;
    _textField_vcode.keyboardType = UIKeyboardTypeNumberPad;
    _textField_vcode.placeholder = @"请输入6位验证码";
    [_textField_vcode addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    
    _textField_password = [UITextField new];
    _textField_password.delegate = self;
    _textField_password.font = Font(16);
    _textField_password.secureTextEntry = YES;
    _textField_password.borderStyle = UITextBorderStyleNone;
    _textField_password.placeholder = @"请输入6-16位登录密码";
    [_textField_password addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    
    _textField_passwordConfirm = [UITextField new];
    _textField_passwordConfirm.delegate = self;
    _textField_passwordConfirm.font = Font(16);
    _textField_passwordConfirm.secureTextEntry = YES;
    _textField_passwordConfirm.borderStyle = UITextBorderStyleNone;
    _textField_passwordConfirm.placeholder = @"请确认新登录密码";
    [_textField_passwordConfirm addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    
    _textField_inviter = [UITextField new];
    _textField_inviter.delegate = self;
    _textField_inviter.font = Font(16);
    _textField_inviter.borderStyle = UITextBorderStyleNone;
    _textField_inviter.keyboardType = UIKeyboardTypeNumberPad;
    _textField_inviter.placeholder = @"请输入邀请人手机号(选填)";
    [_textField_inviter addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    
    
    [_backgroundView_nickname addSubview:_textField_nickname];
    [_backgroundView_phone addSubview:_textField_phone];
    [_backgroundView_vcode addSubview:_textField_vcode];
    [_backgroundView_password addSubview:_textField_password];
    [_backgroundView_passwordConfirm addSubview:_textField_passwordConfirm];
    [_backgroundView_inviter addSubview:_textField_inviter];
    
    [_textField_nickname mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_titleLabel_nickname.mas_right);
        make.top.bottom.mas_equalTo(_backgroundView_nickname);
        make.right.mas_equalTo(_backgroundView_nickname).offset(-30*Scale_W);
    }];
    
    [_textField_phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_titleLabel_phone.mas_right);
        make.top.bottom.mas_equalTo(_backgroundView_phone);
        make.right.mas_equalTo(_backgroundView_phone).offset(-30*Scale_W);
    }];
    
    [_textField_vcode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_titleLabel_vcode.mas_right);
        make.top.bottom.mas_equalTo(_backgroundView_vcode);
        make.right.mas_equalTo(_backgroundView_vcode).offset(-130*Scale_W);
    }];
    
    [_textField_password mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_titleLabel_password.mas_right);
        make.top.bottom.mas_equalTo(_backgroundView_password);
        make.right.mas_equalTo(_backgroundView_password).offset(-30*Scale_W);
    }];
    
    [_textField_passwordConfirm mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_titleLabel_passwordConfirm.mas_right);
        make.top.bottom.mas_equalTo(_backgroundView_passwordConfirm);
        make.right.mas_equalTo(_backgroundView_passwordConfirm).offset(-30*Scale_W);
    }];
    
    [_textField_inviter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_titleLabel_inviter.mas_right);
        make.top.bottom.mas_equalTo(_backgroundView_inviter);
        make.right.mas_equalTo(_backgroundView_inviter).offset(-30*Scale_W);
    }];
    
    
    _vcodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _vcodeButton.enabled = NO;
    _vcodeButton.titleLabel.font = Font(15);
    _vcodeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [_vcodeButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_vcodeButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [_vcodeButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    [_vcodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_vcodeButton addTarget:self action:@selector(vcodeButtonTouchEvent:) forControlEvents:UIControlEventTouchUpInside];
    
    [_backgroundView_vcode addSubview:_vcodeButton];
    
    [_vcodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_textField_vcode.mas_right);
        make.top.bottom.mas_equalTo(_backgroundView_vcode);
        make.right.mas_equalTo(_backgroundView_vcode).offset(-30*Scale_W);
    }];
    
    _registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _registerButton.enabled = NO;
    _registerButton.layer.cornerRadius = 5*Scale_W;
    _registerButton.layer.masksToBounds = YES;
    _registerButton.titleLabel.font = Font(15);
    [_registerButton setTitle:@"注  册" forState:UIControlStateNormal];
    [_registerButton setBackgroundImage:[UIImage imageWithColor:LightGrayColor] forState:UIControlStateDisabled];
    [_registerButton setBackgroundImage:[UIImage imageWithColor:LightGrayColor] forState:UIControlStateSelected];
    [_registerButton setBackgroundImage:[UIImage imageWithColor:kBaseRedColor] forState:UIControlStateNormal];
    [_registerButton setTitleColor:WhiteColor forState:UIControlStateNormal];
    [_registerButton setTitleColor:GrayColor forState:UIControlStateDisabled];
    [_registerButton setTitleColor:GrayColor forState:UIControlStateSelected];
    [_registerButton addTarget:self action:@selector(registerButtonTouchEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_registerButton];
    
    [_registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30*Scale_W);
        make.right.mas_equalTo(-30*Scale_W);
        make.height.mas_equalTo(40*Scale_W);
        make.top.mas_equalTo(_backgroundView_inviter.mas_bottom).offset(20*Scale_W);
    }];
    
}

- (void)textFieldDidChanged:(UITextField *)textField {
    
    NSString *timeOut_get = [NSString calculateNumberOne:[[NSUserDefaults standardUserDefaults] objectForKey:@"timeOut_register"] calculateType:CalculateType_Subtract numberTwo:[NSString getTimestamp]];
    
    if (textField == _textField_phone || textField == _textField_inviter) {
        if (_textField_phone.text.length == 11 && timeOut_get.integerValue < 0) {
            _vcodeButton.enabled = YES;
        } else{
            _vcodeButton.enabled = NO;
        }
    }
    
    if (_textField_password.text.length >= 6 &&
        _textField_passwordConfirm.text.length >= 6 &&
        _textField_vcode.text.length == 6 &&
        _textField_phone.text.length == 11) {
        _registerButton.enabled = YES;
    } else {
        _registerButton.enabled = NO;
    }
}

#pragma mark - 控制字符输入长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSMutableString *text = [NSMutableString stringWithString:textField.text];
    [text replaceCharactersInRange:range withString:string];
    if (textField == _textField_phone || textField == _textField_inviter) {
        return (text.length > 11 || ![string isOnlyNumber]) ? NO : YES;
    } else if (textField == _textField_vcode) {
        return (text.length > 6 || ![string isOnlyNumber]) ? NO : YES;
    } else if (textField == _textField_password || textField == _textField_passwordConfirm) {
        return text.length > 16 ? NO : YES;
    } else if (textField == _textField_nickname) {
        return text.length > 20 ? NO : YES;
    }
    return YES;
}

#pragma mark -
- (void)vcodeButtonTouchEvent:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(getvcodeWithPhone:)]) {
        [self.delegate getvcodeWithPhone:_textField_phone.text];
    }
}

#pragma mark -
- (void)registerButtonTouchEvent:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(registerWithNickName:phone:vcode:password_new:password_confirm:inviter:)]) {
        [self.delegate registerWithNickName:_textField_nickname.text phone:_textField_phone.text vcode:_textField_vcode.text password_new:_textField_password.text password_confirm:_textField_passwordConfirm.text inviter:_textField_inviter.text];
    }
}

- (void)setPhoneString:(NSString *)phoneString {
    _phoneString = phoneString;
    _textField_phone.text = phoneString;
    _vcodeButton.enabled = [phoneString isMobileNumber];
}


@end
